<?php
namespace globalunit\model;
use globalunit\utils\EsClient;
use globalunit\validator\Factory as fc;
use utils\Utils;
use QYS\Log\Log;

class EsPOB
{

    protected $_host = null;

    protected $_port = null;

    protected $_index = null;

    protected $_type = null;

    protected $_id = null;

    protected $_config = null;

    protected $_esClient = false;

    public function __construct($config)
    {
        $this->_config = $config;
        $this->_esClient = new EsClient($this->_config);
    }

    public function insert()
    {
        $obj = $this->itermember();

        $ret = $this->_esClient->add($obj);
        return $ret;
    }
    /**获得单条
     * @param $where
     * @return bool
     */
    public function get($where)
    {
        $data = $this->_esClient->get($where);
        if(!empty($data)){
            $member = $this->itermember();
            foreach ($member as $k => $val){
                if(isset($data[$k])){
                    $tp = gettype($this->$k);
                    switch($tp){
                        case "integer":
                            $this->$k = IntVal($data[$k]);
                            break;
                        default:
                            $this->$k= (String)$data[$k];
                            break;
                    }
                }
            }
            return true;
        }
        return false;
    }

    /**模型对象转换成哈希表
     * @return array
     */
    public function toArray()
    {
        $a = $this->itermember();
        return $a;
    }

    public function getChildModel() {
        return new static($this->_config);
    }

    public function save(){
        $member = $this->itermember();
        return $this->_esClient->save($this->id, $member);
    }

    public function delete($id)
    {
        return $this->_esClient->del($id);
    }

    public function drop()
    {
        return $this->_esClient->drop();
    }

    public function count($query){
        return $this->_esClient->count($query);
    }

    public function select($param)
    {
        $res = $this->_esClient->search($param);
        if(!empty($res)){
            $list = [];
            $i = 0;
            foreach($res as $k => $v){
                $list[$i]['id'] =  IntVal($v['_id']);
                $list[$i]['score'] = empty($v['_score'])? (String)0:(String)($v['_score']);
                foreach ($this as $key => $value){
                    if (substr($key, 0, 1) != "_" && $key != 'id') {
                        $list[$i][$key] = (String)$v['_source'][$key];
                    }
                }
                $i ++;
            }
            return $list;
        }
        return $res;
    }

    protected function itermember()
    {
        $a = array();
        foreach ($this as $key => $value) {
            if (substr($key, 0, 1) != "_") {
                if(gettype($value)=='integer'){
                    $a[$key] = IntVal($value);
                }else{
                    $a[$key] = (String)$value;
                }
            }
        }
        return $a;
    }

}
